<template>
  <!--折线图-->
  <div
    :style="{width:width,height:height}"
  ></div>
</template>

<script>
  export default {
    name: 'FourBarChart',
    props: {
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      colors: {
        type: String,
        default: 'rgba(4, 203, 250'
      },
      /**
       * @param:
       *    dataLine  --> 折线的数据
       *    dataY  --> Y轴的数据
       * @date 2020/08/23
       * @author ZhuHongWei
       */
      chartData: {
        type: Object,
        default: () => ({
          valueList: [],
          dataList: [],
          bgList: []
        })
      },
      barColor: {
        type: String,
        default: '#58A3F7'
      },
      title: {
        type: String,
        default: ''
      },
      dataList: {
        type: Array,
        default: ()=>[]
      },
      valueList: {
        type: Array,
        default: ()=>[]
      },
      bgList: {
        type: Array,
        default: ()=>[]
      }
    },
    data() {
      return {
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler: function() {
          this.initChart()
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
      this.$bus.$off('barInit')
      this.$bus.$on('barInit', this.initChart)
    },
    methods: {
      initChart() {
        let that = this
        this.chart = this.$echarts.init(this.$el)
        this.chart.clear()
        let option = {
          tooltip: {
            trigger: 'item',
            formatter:function(val){
              return `${val.name}: ${val.value}分`
            },
          },
          grid: {
            left: '2%',
            right: '2%',
            bottom: '0%',
            top: '50px',
            containLabel: true
          },
          title: {
            show: false
          },
          // dataZoom: [
          //   {
          //     type: 'slider',
          //     realtime: true,
          //     yAxisIndex: [0],
          //     start: 0,
          //     end: 100
          //   }
          // ],
          yAxis: [
            {
              name: this.title,
              nameTextStyle: {
                color: '#FFFFFF',
                padding: [0, 10, 10, 70],
                fontSize: 16
              },
              nameLocation: 'start',
              type: 'category',
              inverse: true,
              axisLine: { // y轴
                show: false
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                show: false,
                inside: false
              }
            },
            {
              name: '得分',
              inverse: true,
              nameTextStyle: {
                color: '#FFFFFF',
                padding: [0, 10, 10, 0],
                fontSize: 16
              },
              type: 'category',
              axisTick: 'none',
              axisLine: 'none',
              show: true,
              axisLabel: {
                textStyle: {
                  color: '#9aeced',
                  fontSize: '12'
                }
              },
              data: this.chartData.valueList
            }
          ],
          xAxis: [
            {
              type: 'value',
              axisTick: {
                show: false
              },
              axisLine: {
                show: false
              },
              splitLine: {
                show: false
              },
              axisLabel: {
                show: false
              }
            }
          ],
          series: [
            {
              name: '',
              type: 'bar',
              barWidth: 8, // 柱子宽度
              zlevel: 1,
              itemStyle: {
                normal: {
                  show: true,
                  barBorderRadius: [0, 4, 4, 0], // 圆角（左上、右上、右下、左下）
                  color: function (params) {
                    let op = (params.value/100).toFixed(1)
                    return new that.$echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                      offset: 0,
                      color: `${that.colors}, 0)`
                    }, {
                      offset: 1,
                      color: `${that.colors}, ${op})`
                    }], false) // 渐变
                  }
                }
              },
              label: {
                normal: {
                  color: '#b3ccf8',
                  show: true,
                  position: [0, '-14px'],
                  textStyle: {
                    fontSize: 14
                  },
                  formatter: function (a, b) {
                    return a.name
                  }
                }
              },
              position: [0, '-24px'],
              data: this.chartData.dataList
            },
            {
              name: '背景',
              type: 'bar',
              barWidth: 8,
              barGap: '-100%',
              data: this.chartData.bgList,
              itemStyle: {
                normal: {
                  color: 'rgba(24,31,68,1)',
                  barBorderRadius: [0, 20, 20, 0], // 圆角（左上、右上、右下、左下）
                }
              },
            }
          ]
        }
        this.chart.setOption(option)
      }
    }
  }
</script>

<style scoped>
</style>
